package sis.myjunit;/*
 * @auther 顶风少年
 * @mail dfsn19970313@foxmail.com
 * @date 2019-08-03 17:21
 * @notify
 * @version 1.0
 */

import java.lang.reflect.Method;
import java.util.HashSet;
import java.util.Set;

public class TestRunnerTest {

    private TestRunner runner;
    private static final String methodNameA = "testA";
    private static final String methodNameB = "testB";
    private static final String methodNameC= "testC";
    @Test
    public void singleMethodTest() {
        runTests(SingleMethodTest.class);
        verifyTests(methodNameA);
    }

    @Test
    public void multipleMethodTest() {
        runTests(MultipleMethodTest.class);
        verifyTests(methodNameA, methodNameB);
        verifyIgnoreMethodNames(methodNameC);
    }

    private void runTests(Class testClass) {
        runner = new TestRunner(testClass);
        runner.run();
    }

    private void verifyTests(String... expectedTestMethodNames) {
        verifyNumberOfTests(expectedTestMethodNames);
        verifyMethodNames(expectedTestMethodNames);
        verifyCounts(expectedTestMethodNames);
    }



    //检验方法个数
    private void verifyNumberOfTests(String... testMethodNames) {
        assert testMethodNames.length == runner.getTestMethods().size() :
                "应该有" + testMethodNames.length + "个方法,实际有" + runner.getTestMethods().size();
    }

    //检验方法名
    private void verifyMethodNames(String... testMethodNames) {
        Set<String> actualMethodNames = getTestMethodNames();
        for (String methodName : testMethodNames) {
            assert actualMethodNames.contains(methodName) :
                    "方法列表不包含" + methodName + "方法";
        }
    }

    //检验方法运行成功次数
    private void verifyCounts(String... testMethodNames) {
        assert testMethodNames.length == runner.passed() :
                "有" + testMethodNames.length + "个方法,运行成功" + runner.passed();
        assert 0 == runner.failed() : "有" + testMethodNames.length + "个方法,运行失败" + runner.passed();
    }

    //检验忽略方法
    private void verifyIgnoreMethodNames(String... testMethodNames){
        assert testMethodNames.length == runner.getIgnoredMethods().size() :
                "应该有" + testMethodNames.length + "个方法,被忽略,实际"+runner.getIgnoredMethods().size()  ;
    }

    //遍历方法列表,获取方法名列表
    private Set<String> getTestMethodNames() {
        Set<String> methodNames = new HashSet<>();
        for (Method method : runner.getTestMethods()) {
            methodNames.add(method.getName());
        }
        return methodNames;
    }


//
//    public void singleMethodTest() {
//        TestRunner runner = new TestRunner(SingleMethodTest.class);
//
//        //断言 SingleMethodTest类中有一个方法
//        Set<Method> testMethods = runner.getTestMethods();
//        assert 1 == testMethods.size() : "长度断言失败";
//
//        //取出第一个方法的名称
//        Iterator<Method> it = testMethods.iterator();
//        Method method = it.next();
//        final String testMethodName = "testA";
//
//        //断言名称
//        assert testMethodName.equals(method.getName()) :
//                "名称 " + testMethodName + "断言失败";
//
//        //运行方法
//        runner.run();
//
//        //断言成功一次
//        assert 1 == runner.passed() : "运行成功断言失败";
//        //断言失败0次
//        assert 0 == runner.failed() : "运行失败断言失败";
//    }
//
//    public void multipleMethodTest() {
//        TestRunner runner = new TestRunner(MultipleMethodTest.class);
//        runner.run();
//        //断言 MultipleMethodTest运行成功两个
//        assert 2 == runner.passed() : "断言 MultipleMethodTest运行成功两个";
//        ////断言 MultipleMethodTest类运行失败 0个
//        assert 0 == runner.failed() : "断言 MultipleMethodTest类运行失败 0个";
//        //方法列表
//        Set<Method> testMethods = runner.getTestMethods();
//        //断言方法个数
//        assert 2 == testMethods.size() : "断言方法个数";
//
//        //存储方法名字
//        Set<String> methodNames = new HashSet<String>();
//        //循环方法名并添加
//        for (Method method : testMethods) {
//            methodNames.add(method.getName());
//        }
//
//        final String testMethodNameA = "testA";
//        final String testMethodNameB = "testB";
//
//        //断言包含方法名
//        assert methodNames.contains(testMethodNameA) :
//                "断言包含方法名 " + testMethodNameA;
//        assert methodNames.contains(testMethodNameB) :
//                "断言包含方法名 " + testMethodNameB;
//    }

}
